﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using WL_Store.Model;

namespace WL_Store.DAL
{
   public  class LoginLogsDAL
    {
        /// <summary>
        /// 查询所有登录日志
        /// </summary>
        /// <returns></returns>
        public List<LoginLogsModel> GetLoginLogs()
        {
            string sql = @"select*from LoginLogs";

            SqlDataReader reader = Dbhelper.ExecuteReader(sql);
            List<LoginLogsModel> listModel = new List<LoginLogsModel>();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    LoginLogsModel model = new LoginLogsModel();
                    model.LogId = Convert.ToInt32(reader["LogId"]);
                    model.LoginName = reader["LoginName"].ToString();
                    model.AdminName = reader["AdminName"].ToString();
                    model.ServerName = reader["ServerName"].ToString();
                    model.ClientIP = reader["ClientIP"].ToString();
                    model.LoginTime = Convert.ToDateTime(reader["LoginTime"]);
                    if (reader["ExitTime"].ToString() == "")
                    {
                        model.ExitTime = null;
                    }
                    else
                    {
                        model.ExitTime = Convert.ToDateTime(reader["ExitTime"]);
                    }
                    //model.ExitTime = reader["ExitTime"].ToString() == "" ? null : Convert.ToDateTime(reader["ExitTime"]);
                    listModel.Add(model);
                    
                }
                reader.Close();
            }
            return listModel;

        }

        /// <summary>
        /// 添加登录日志
        /// </summary>
        /// <returns></returns>
        public bool AddLonginLog(LoginLogsModel model)
        {
            string sql = @"insert into LoginLogs(LoginName,AdminName,ServerName,ClientIP,LoginTime) values
                          (@LoginName,@AdminName,@ServerName,@ClientIP,@LoginTime)";
            SqlParameter[] parameter = { 
            new SqlParameter("@LoginName",model.LoginName),
            new SqlParameter("@AdminName",model.AdminName),
            new SqlParameter("@ServerName",model.ServerName),
            new SqlParameter("@ClientIP",model.ClientIP),
            new SqlParameter("@LoginTime",model.LoginTime)
            };

            return Dbhelper.ExecuteNonQuery(sql, parameter) > 0;
        }

        /// <summary>
        /// 根据登录名查询，查询姓名
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public object GetName(string loginName)
        {
            string sql = @"select name from AdminInfo where loginName=@loginName";
            SqlParameter[] parameters =
                { 
            new SqlParameter("@loginName",loginName)
            };
           return  Dbhelper.ExecuteScalar(sql, parameters);
        }

        /// <summary>
        /// 将用户的退出时间保存在日志中
        /// </summary>
        /// <param name="logId"></param>
        /// <param name="exitTime"></param>
        /// <returns></returns>
        public bool UpdateExitTime(int logId ,DateTime exitTime)
        {
            string sql = @"update LoginLogs set ExitTime=@exitTime where LogId=@logId";
            SqlParameter[] parameters =
            {
                new SqlParameter("@logId",logId),
                new SqlParameter("@exitTime",exitTime)
            };
            return Dbhelper.ExecuteNonQuery(sql,parameters)>0;
        }
        /// <summary>
        /// 获取日志自增LogId的当前值
        /// </summary>
        /// <returns></returns>
        public object GetIdentity()
        {
            string sql = @"SELECT IDENT_CURRENT('LoginLogs')";
           return  Dbhelper.ExecuteScalar(sql);
        }
    }
}
